package com.chuangsi.project.admin.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangsi.common.exception.ServiceException;
import com.chuangsi.framework.web.controller.BaseController;
import com.chuangsi.framework.web.domain.ResultObject;
import com.chuangsi.framework.web.page.TableDataInfo;
import com.chuangsi.project.admin.domain.ModelTable;
import com.chuangsi.project.admin.dto.admin.brand.ReqBrandUpdateDto;
import com.chuangsi.project.admin.dto.admin.model.*;
import com.chuangsi.project.admin.dto.common.SelectDto;
import com.chuangsi.project.admin.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangshengming
 * @date 2023/3/23
 */
@Api(tags = "型号管理")
@RestController
@RequestMapping("/adminApi/model")
public class ModelController  extends BaseController {

    @Autowired
    private ModelService modelService;

    @ApiOperation(value = "型号分页",notes = "王圣铭")
    @GetMapping("/modelPage")
    public TableDataInfo<ResModelPageDto> modelPage(ReqModelPageDto req){
        List<ResModelPageDto> page = modelService.modelPage(req);
        return getDataTable(page);
    }

    @ApiOperation(value = "新增型号",notes = "王圣铭")
    @PostMapping("/modelAdd")
    public ResultObject<Void> modelAdd(@RequestBody @Valid ReqModelAddDto req){
        ModelTable modelTable = new ModelTable();
        modelTable.setModelName(req.getModelName());
        boolean flag = modelService.save(modelTable);
        if(flag){return ResultObject.ok("新增成功");}
        else {return ResultObject.error("新增失败");}
    }

    @ApiOperation(value = "修改型号",notes = "王圣铭")
    @PostMapping("/modelUpdate")
    public ResultObject<Void> modelUpdate(@RequestBody @Valid ReqModelUpdateDto req){
        ModelTable modelTable = modelService.getById(req.getModelId());
        if(modelTable ==null){
            throw new ServiceException("未查询到型号详情");
        }
        modelTable.setModelName(req.getModelName());
        boolean flag = modelService.updateById(modelTable);
        if(flag){return ResultObject.ok("新增成功");}
        else {return ResultObject.error("新增失败");}
    }

    @ApiOperation(value = "型号详情",notes = "王圣铭")
    @PostMapping("/modelDetail")
    public ResultObject<ReqModelUpdateDto> modelDetail(@RequestBody @Valid ReqModelDetailDto req){
        ModelTable modelTable = modelService.getById(req.getModelId());
        if(modelTable ==null){
            throw new ServiceException("未查询到型号详情");
        }
        ReqModelUpdateDto res = new ReqModelUpdateDto();
        res.setModelId(modelTable.getModelId());
        res.setModelName(modelTable.getModelName());
        return ResultObject.ok("查询成功").setData(res);
    }

    @ApiOperation(value = "型号删除",notes = "王圣铭")
    @PostMapping("/modelDel")
    public ResultObject<Void> modelDel(@RequestBody @Valid ReqModelDetailDto req){
        modelService.removeById(req.getModelId());
        return ResultObject.ok("删除成功");
    }

    @ApiOperation(value = "型号下拉选",notes = "王圣铭")
    @PostMapping("/modelSelect")
    public ResultObject<List<SelectDto>> modelSelect(){
        List<SelectDto> list = modelService.modelSelect();
        return ResultObject.ok("查询成功").setData(list);
    }

}
